package com.hfzy.ihk.web.oauth.server.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.session.FindByIndexNameSessionRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义登出接口
 */
@Controller
@Slf4j
public class LogoutController {



    @Value("${security.oauth2.sso.system-index-url}")
    private String systemIndexUrl ;


    /**
     * 登出桥接接口，客户端登出以后调用该接口删除验证中心的数据
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("exit")
    public void exit(HttpServletRequest request, HttpServletResponse response,String callback) {
        new SecurityContextLogoutHandler().logout(request, null, null);
        try {
            //跳回回调的地址
            log.info("==========》登出回调跳转地址："+request.getHeader("referer"));
            if(systemIndexUrl!=null){
                response.sendRedirect(systemIndexUrl);
            }else{
                response.sendRedirect(request.getHeader("referer"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
